package com.fbs.wowo.util.dbutils;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Sorts;

import java.util.ArrayList;
import java.util.List;

public class MongoUtils {

    public static List find(String collectionName, Integer pageNo, Integer pageSize) {
        int skip = (pageNo - 1) * pageSize;
        MongoCollection transactions = MongoConfig.getCollection(collectionName);
        // .sort(Filters.eq("_id", -1))
        FindIterable findIterable = transactions.find().sort(Sorts.descending("_id")).skip(skip).limit(pageSize);
        List list = new ArrayList();
        findIterable.into(list);
        return list;
    }

    public static Object findOne(String collectionName, String key, String value) {
        MongoCollection transactions = MongoConfig.getCollection(collectionName);
        Object obj = transactions.find(Filters.eq(key, value)).first();
        return obj;
    }
}
